/*Sean waitkus CSC 362 Project 3
This program takes in an input file and populates an array character by character using pointer arithmetic.
It than first outputs the encrypted message followed by the decrypted message to the console*/

#include "stdafx.h"

void main(){
	/*defines and declares the encrypt array, the size is defined in the header*/
	int encrypt [encryptLength] = {5, 2, -1, 2, -3, 2, -4, -2, 6, 4};
	/*creates the input file pointer*/
	FILE *ifp;
	/*declares the input array and its length based on a constant in the header*/
	char input[arrayLength];
	/*opens the input file*/
	ifp = fopen( "message1.txt", "r");

	/*function calls*/
	/*calls the input function and passes it the input file and input array*/
	Input(ifp, input);
	/*calls the encrypt function passing it the input and encryption array*/
	Encrypt(input, encrypt);
	/*calls the output function to output the encrypted array passing it the input array*/
	Output(input);
	/*calls the decrypt function and passes it the inut and encryption arrays*/
	Decrypt(input, encrypt);
	/*calls the output function to output the decrypted array, passing it the input array*/
	Output(input);

	/*closes the input file*/
	fclose(ifp);

}

/*takes in the input file and input array and populates the input array*/
void Input( FILE * ifp, char input []){
	/*int variable for file reading*/
	int c = 0;
	/*char pointer for array traversal*/
	char *z;
	/*sets the pointer z to the first index in the input array*/
	z = &input[0];
	/*loops through the file until c indicates EOF and assigns the value for c 
	to the current index in the input array and increments *z.*/
	while(c != EOF){
		c = getc(ifp);
		/*Once c indicates EOF appends '\0' to the array to indicate the end of the string*/
		if(c == EOF){
			*z = '\0';
		}
		/*otherwise set the pointer for the input array to the value of c and increment the pointer*/
		else{
			*z = c;
			z++;
		}
	}
}//end Input function

/*ouptuts input array to the console*/
void Output(FILE *ofp, char input[]){
	/*pointer for the input array*/
	char *c;
	/*console halt variable*/
	int n;
	/*sets the pointer *c to the first index in the input array*/
	c = &input[0];
	while( *c != '\0'){
		/*prints the current character to the console and increments *c*/
		putchar(*c);
		c++;
	}
	/*halts the console window until input is recieved*/
	scanf("%d", &n);
	
}//end output function

/*Modifies and encrypts the input array based on the values stored in the encrypt array*/
void Encrypt(char input[] , int encrypt[]){
	
	/*pointer for the string array*/
	char *c;
	/*pointer for the encrytpion array*/
	int *eip;
	/*sets the pointer to the first index in the array*/
	c = &input[0];

	/*sets the pointer to the first index of the encryption array*/
	eip = &encrypt[0];

	/*loops through and encrypts the array until *c indicates the end of the string/array*/
	while(*c != '\0'){	

		/*checks if *c is an upper case letter and if true performs the encryption*/
		if(isalpha(*c) && isupper(*c)){
			
			/*encrypts *c based on the current index of the encryption array*/
			*c = *c + *eip;

			/*checks if the character at *c is greater than the ASCII value of 'Z' after modification
			if true decreases the ASCII value by 26 so that the modified value is still a letter*/
			if(*c  > 'Z'){
				*c = *c - 26;
			
			}
			/*checks if the character at *c is less than the ASCII value of 'A' after modification
			if true increases the ASCII value by 26 so that the modified value is still a letter*/
			else if (*c  < 'A'){
				*c = *c + 26;
			}
			
			/*checks if *eip exceeded the index of the encryption array if true resets
			*eip to point to first index in the encryption array and increments *c 
			else increment *c and *eip */
			if(*eip == encrypt[encryptLength - 1]){
				eip = &encrypt[0];
				c++;
				
			}
			else{
				eip++;
				c++;
				
			}
		}
		/*checks if *c is a lower case letter and if true performs the encryption*/
		else if(isalpha(*c) && islower(*c)){
			
			/*encrypts *c based on the current index of the encryption array*/
			*c = *c + *eip;
			
			/*checks if the character at *c is greater than the ASCII value of 'z' after encryption
			if true decreases the ASCII value by 26 so that the modified value is still a letter*/
			if(*c > 'z'){
				*c = *c - 26;
				
			}
			/*checks if the character at *c is less than the ASCII value of 'a' after ecryption
			if true increases the ASCII value by 26 so that the modified value is still a letter*/
			else if (*c < 'a'){
				*c = *c + 26;
			}
			/*checks if *eip exceeded the index of the encryption array if true resets
			*eip to point to first index in the encryption array and increments *c 
			else increment *c and *eip */
			if(*eip == encrypt[encryptLength - 1]){
				eip = &encrypt[0];
				c++;
			}
			else{
				eip++;
				c++;
			}
		}
		/* if *c is not a letter increment *c to point to the next index*/
		else {
			c++;
		}
	}
}//end encryption function

/*Modifies and decrypts the input array based on the values stored in the encrypt array*/
void Decrypt(char input[] , int encrypt[]){
/*pointer for the string array*/
	char *c;
	/*pointer for the encrytpion array*/
	int *eip;
	/*sets the pointer to the first index in the array*/
	c = &input[0];
	/*sets the pointer to the first index of the encryption array*/
	eip = &encrypt[0];

	/*loops through and encrypts the array until *c indicates the end of the string/array*/
	while(*c != '\0'){	

		/*checks if *c is an upper case letter and if true performs the encryption*/
		if(isalpha(*c) && isupper(*c)){
			
			/*encrypts *c based on the current index of the encryption array*/
			*c = *c - *eip;

			/*checks if the character at *c is greater than the ASCII value of 'Z' after modification
			if true decreases the ASCII value by 26 so that the modified value is still a letter*/
			if(*c  > 'Z'){
				*c = *c - 26;
			
			}
			/*checks if the character at *c is less than the ASCII value of 'A' after modification
			if true increases the ASCII value by 26 so that the modified value is still a letter*/
			else if (*c  < 'A'){
				*c = *c + 26;
			}
			
			/*checks if *eip exceeded the index of the encryption array if true resets
			*eip to point to first index in the encryption array and increments *c 
			else increment *c and *eip */
			if(*eip == encrypt[encryptLength - 1]){
				eip = &encrypt[0];
				c++;
				
			}
			else{
				eip++;
				c++;
				
			}
		}
		/*checks if *c is a lower case letter and if true performs the encryption*/
		else if(isalpha(*c) && islower(*c)){
			
			/*encrypts *c based on the current index of the encryption array*/
			*c = *c - *eip;
			
			/*checks if the character at *c is greater than the ASCII value of 'z' after encryption
			if true decreases the ASCII value by 26 so that the modified value is still a letter*/
			if(*c > 'z'){
				*c = *c - 26;
				
			}
			/*checks if the character at *c is less than the ASCII value of 'a' after ecryption
			if true increases the ASCII value by 26 so that the modified value is still a letter*/
			else if (*c < 'a'){
				*c = *c + 26;
			}
			/*checks if *eip exceeded the index of the encryption array if true resets
			*eip to point to first index in the encryption array and increments *c 
			else increment *c and *eip */
			if(*eip == encrypt[encryptLength - 1]){
				eip = &encrypt[0];
				c++;
			}
			else{
				eip++;
				c++;
			}
		}
		/* if *c is not a letter increment *c to point to the next index*/
		else {
			c++;
		}
	}
}//end decrypt function
	
	

